﻿Imports DevComponents.Editors.DateTimeAdv
Imports DevComponents.DotNetBar.Controls
Imports DevComponents.DotNetBar

Public Class ucCommands
    Private _ucParent As UserControl
    Private _exceptControlList As List(Of Control)

    'Public Sub New()

    '    ' This call is required by the designer.
    '    InitializeComponent()

    'End Sub

    'Public Sub New(ByRef uc As UserControl, Optional ByRef exceptControlList As List(Of Control) = Nothing)

    '    ' This call is required by the designer.
    '    InitializeComponent()

    '    ' Add any initialization after the InitializeComponent() call.
    '    _ucParent = uc
    '    _exceptControlList = exceptControlList
    'End Sub

    Public Sub InitControl(ByRef uc As UserControl, Optional ByRef exceptControlList As List(Of Control) = Nothing)
        ' Add any initialization after the InitializeComponent() call.
        _ucParent = uc
        _exceptControlList = exceptControlList
        EnableButtons(False)
    End Sub

    ''' <summary>
    ''' Cho biết nút nào hiện/ẩn
    ''' </summary>
    ''' <param name="isShowAdd"></param>
    ''' <param name="isShowEdit"></param>
    ''' <param name="isShowDelete"></param>
    ''' <remarks></remarks>
    Public Sub ShowHideButtons(ByVal isShowAdd As Boolean, ByVal isShowEdit As Boolean, ByVal isShowDelete As Boolean, Optional ByVal isShowRefresh As Boolean = False)
        If Not isShowAdd Then
            btnAdd.Visible = False
            btnRefresh.Location = btnCancel.Location
            btnCancel.Location = btnUpdate.Location
            btnUpdate.Location = btnDel.Location
            btnDel.Location = btnEdit.Location
            btnEdit.Location = btnAdd.Location
        End If

        If Not isShowEdit Then
            btnEdit.Visible = False
            btnRefresh.Location = btnCancel.Location
            btnCancel.Location = btnUpdate.Location
            btnUpdate.Location = btnDel.Location
            btnDel.Location = btnEdit.Location
        End If

        If Not isShowDelete Then
            btnDel.Visible = False
            btnRefresh.Location = btnCancel.Location
            btnCancel.Location = btnUpdate.Location
            btnUpdate.Location = btnDel.Location
        End If

        If Not isShowRefresh Then
            btnRefresh.Visible = False
        End If
    End Sub


    Private Sub EnableControls(ByRef controlCollect As ControlCollection, ByVal val As Boolean)
        For Each c As Control In controlCollect
            If c.Controls.Count > 0 Then
                If _exceptControlList IsNot Nothing Then
                    If Not _exceptControlList.Contains(c) Then
                        EnableControls(c.Controls, val)
                    Else
                        Dim str As String = "Equal"
                    End If
                Else
                    EnableControls(c.Controls, val)
                End If
            End If

            'Visual Studio Controls
            If TypeOf c Is TextBox Then
                CType(c, TextBox).ReadOnly = Not val
                'If c.Name.Contains("Search") Then
                '    CType(c, TextBox).ReadOnly = False
                'Else
                '    CType(c, TextBox).ReadOnly = Not val
                'End If
            ElseIf TypeOf c Is RichTextBox Then
                CType(c, RichTextBox).ReadOnly = Not val
                'If c.Name.Contains("Search") Then
                '    CType(c, RichTextBox).ReadOnly = False
                'Else
                '    CType(c, RichTextBox).ReadOnly = Not val
                'End If
            ElseIf (TypeOf c Is LinkLabel) Then
                CType(c, LinkLabel).Enabled = val
            ElseIf (TypeOf c Is DateTimeInput) Then
                CType(c, DateTimeInput).Enabled = val
            ElseIf (TypeOf c Is GroupBox) Then
                CType(c, GroupBox).Enabled = val
            ElseIf (TypeOf c Is CheckBox) Then
                CType(c, CheckBox).Enabled = val
            ElseIf (TypeOf c Is Button) Then
                Dim btn As Button = CType(c, Button)
                If Not btn.Text.Contains("Add") Or Not btn.Text.Contains("Edit") Or Not btn.Text.Contains("Del") Or Not btn.Text.Contains("Print") Then
                    CType(c, Button).Enabled = val
                End If

            End If

            'DotNetBar Controls
            If TypeOf c Is TextBoxX Then
                CType(c, TextBoxX).ReadOnly = Not val
                'If c.Name.Contains("Search") Then
                '    CType(c, TextBoxX).ReadOnly = False
                'Else
                '    CType(c, TextBoxX).ReadOnly = Not val
                'End If
            ElseIf (TypeOf c Is ComboTree) Then
                CType(c, ComboTree).Enabled = val
            ElseIf (TypeOf c Is ComboBoxEx) Then
                CType(c, ComboBoxEx).Enabled = val
            ElseIf (TypeOf c Is CheckBoxX) Then
                CType(c, CheckBoxX).Enabled = val
            ElseIf (TypeOf c Is ButtonX) Then
                Dim btn As ButtonX = CType(c, ButtonX)
                If Not btn.Text.Contains("Add") And _
                    Not btn.Text.Contains("Edit") And _
                    Not btn.Text.Contains("Del") And _
                    Not btn.Text.Contains("Print") And _
                    Not btn.Name.Contains("btnDetailBooking") And _
                    Not btn.Name.Contains("btnCI") Then
                    CType(c, ButtonX).Enabled = val
                End If
            End If

            ''Luôn luôn visible
            'If c.Name.Contains("Search") Or c.Name.Contains("Refresh") Then
            '    c.Enabled = True
            'End If

            'Luôn luôn visible
            If c.Name.Contains("Refresh") Then
                c.Enabled = True
            End If
        Next
    End Sub

    'Private Sub ResetControls(ByRef controlCollect As Control.ControlCollection)
    '    For Each c As Control In controlCollect
    '        If c.Controls.Count > 0 Then
    '            ResetControls(c.Controls)
    '        End If

    '        'Visual Studio Controls
    '        If TypeOf c Is TextBox Then
    '            CType(c, TextBox).Text = ""
    '        ElseIf TypeOf c Is RichTextBox Then
    '            CType(c, RichTextBox).Text = ""
    '        ElseIf (TypeOf c Is DateTimeInput) Then
    '            CType(c, DateTimeInput).Value = Nothing
    '        ElseIf (TypeOf c Is CheckBox) Then
    '            CType(c, CheckBox).CheckState = CheckState.Indeterminate
    '        End If

    '        'DotNetBar Controls
    '        If TypeOf c Is TextBoxX Then
    '            CType(c, TextBoxX).Text = ""
    '        ElseIf (TypeOf c Is ComboTree) Then
    '            'CType(c, ComboTree).Se
    '        ElseIf (TypeOf c Is ComboBoxEx) Then
    '            CType(c, ComboBoxEx).Text = ""
    '            CType(c, ComboBoxEx).SelectedIndex = -1
    '        ElseIf (TypeOf c Is CheckBoxX) Then
    '            CType(c, CheckBoxX).CheckState = CheckState.Indeterminate
    '        End If
    '    Next
    'End Sub

    ''' <summary>
    ''' Show/hide command buttons add, edit
    ''' </summary>
    ''' <param name="val"></param>
    ''' <param name="rowCount"></param>
    ''' <remarks></remarks>
    Public Sub EnableButtons(ByVal val As Boolean, Optional ByVal rowCount As Integer = 1)
        btnUpdate.Enabled = val
        btnCancel.Enabled = val

        btnAdd.Enabled = Not val
        'Nếu gridview tồn tại grow thì hiển thị chức năng edit và delete
        If rowCount > 0 Then
            btnEdit.Enabled = Not val
            btnDel.Enabled = Not val
        Else 'Ngược lại thì luôn ẩn đi
            btnEdit.Enabled = False
            btnDel.Enabled = False
        End If


        If _ucParent IsNot Nothing Then
            EnableControls(_ucParent.Controls, val)
        End If
    End Sub

    'btnAddEvent_Click = New EventHandler(AddressOf Test)
    'Public Sub Test()

    'End Sub

    Public btnAddEvent_Click As EventHandler
    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
        'ResetControls(_ucParent.Controls)
        EnableButtons(True)

        If btnAddEvent_Click IsNot Nothing Then
            btnAddEvent_Click(sender, e)
        End If
    End Sub

    Public btnEditEvent_Click As EventHandler
    Private Sub btnEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEdit.Click
        EnableButtons(True)

        If btnEditEvent_Click IsNot Nothing Then
            btnEditEvent_Click(sender, e)
        End If
    End Sub

    Public btnDeleteEvent_Click As EventHandler
    Private Sub btnDel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDel.Click
        'ResetControls(_ucParent.Controls)
        If btnDeleteEvent_Click IsNot Nothing Then
            btnDeleteEvent_Click(sender, e)
        End If
    End Sub

    Public btnUpdateEvent_Click As EventHandler
    Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
        EnableButtons(False)

        If btnUpdateEvent_Click IsNot Nothing Then
            btnUpdateEvent_Click(sender, e)
        End If
    End Sub

    Public btnCancelEvent_Click As EventHandler
    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        EnableButtons(False)

        If btnCancelEvent_Click IsNot Nothing Then
            btnCancelEvent_Click(sender, e)
        End If
    End Sub

    Private Sub ucCommands_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'EnableButtons(False)
    End Sub

    Public btnRefreshEvent_Click As EventHandler
    Private Sub btnRefresh_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRefresh.Click
        If btnRefreshEvent_Click IsNot Nothing Then
            btnRefreshEvent_Click(sender, e)
        End If
    End Sub
End Class
